package models;

import play.db.jpa.*;
import play.data.validation.*;

import javax.persistence.*;
import java.util.*;
import java.text.*;
import java.math.*;

@Entity
public class Booking extends Model {
    
    @Required
    @ManyToOne
    public User user;
    
    @Required
    @ManyToOne
    public Komponent komponent;
    
    @Required
    @Temporal(TemporalType.DATE) 
    @InFuture
    public Date checkinDate;
    
    @Required
    @Temporal(TemporalType.DATE)
    @InFuture
    public Date checkoutDate;
    
    @Required
    @MaxSize(180) 
    public String book_note;

    public Booking(Komponent komponent, User user) {
        this.komponent = komponent;
        this.user = user;
    }
   
    //public BigDecimal getTotal() {
    //    return komponent.price.multiply( new BigDecimal( getNights() ) );
    //}

    //public int getNights() {
    //    return (int) ( checkoutDate.getTime() - checkinDate.getTime() ) / 1000 / 60 / 60 / 24;
    //}

    public String getDescription() {
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        return komponent==null ? null : komponent.komp_name + 
            ", " + df.format( checkinDate ) + 
            " to " + df.format( checkoutDate );
    }

    public String toString() {
        return "Booking(" + user + ","+ komponent + ")";
    }

}
